package com.thouiot.test.modbus.controller;

import com.alibaba.fastjson2.JSON;
import com.thouiot.common.result.ResultUtil;
import com.thouiot.common.result.TiotResult;
import com.thouiot.modbus.entity.vo.ModbusLocatorVo;
import com.thouiot.modbus.entity.vo.ReadValueVo;
import com.thouiot.modbus.service.IMasterService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @description modbus master 控制器
 * @author wangkai
 * @date 2025/04/17 09:37
 */
@RestController
@RequestMapping("/modbus-master")
public class ModbusMasterController {

    @Resource
    private IMasterService masterService;

    /**
     * modbus 读取操作
     * @param locator
     * @return
     * @throws Exception
     */
    @PostMapping("/read")
    public TiotResult read(@RequestBody ModbusLocatorVo locator) throws Exception {
        ReadValueVo readValueVo = masterService.readModbus(locator);
        return ResultUtil.builderSuccessResult(readValueVo.getValue());
    }

    /**
     * modbus 写入操作
     * @param locator
     * @return
     * @throws Exception
     */
    @PostMapping("/write")
    public TiotResult write(@RequestBody ModbusLocatorVo locator) throws Exception {
        boolean result = masterService.writeModbus(locator);
        return ResultUtil.builderSuccessResult(result);
    }
}
